package com.wanmait.computershop.dao;

import com.wanmait.computershop.util.DBHelper;
import com.wanmait.computershop.vo.Address;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Random;

public class OrderConfirmDAO {

    public static Address selectByAid(Integer aid)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select *,province_name provinceName,city_name cityName,area_name areaName from address where id = ?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Address.class),aid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static Address selectDefaultByUid(Integer uid)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select *,province_name provinceName,province_code provinceCode," +
                "city_name cityName,city_code cityCode," +
                "area_name areaName,area_name areaCode,is_default isDefault " +
                "from address where uid = ? and is_default=1";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Address.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static List<Address> selectNotDefaultByUid(Integer uid)
    {
        QueryRunner queryRunner = new QueryRunner(DBHelper.getDruidDataSource());
        String sql = "select *,province_name provinceName,province_code provinceCode," +
                "city_name cityName,city_code cityCode," +
                "area_name areaName,area_name areaCode,is_default isDefault " +
                "from address where uid = ? and is_default=0";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Address.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
